package dcutils.rpg;

/**
 * The Vital class represents things like hit points, magic points.<br/>
 * E.g.:
<pre style='color:#d1d1d1;background:#000000;'>
<span style='color:#bb7977; font-weight:bold; '>Vital</span> hp = new <span style='color:#bb7977; font-weight:bold; '>Vital</span>(50)<span style='color:#d2cd86; '>;</span>&lt;br/>
if(hp.reduce(num)) <span style='color:#b060b0; '>{</span><span style='color:#d2cd86; '>&lt;</span>br<span style='color:#d2cd86; '>/</span><span style='color:#d2cd86; '>></span>
   <span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#d2cd86; '>.</span>out<span style='color:#d2cd86; '>.</span>println<span style='color:#d2cd86; '>(</span><span style='color:#00c4c4; '>"still alive"</span><span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span><span style='color:#d2cd86; '>&lt;</span>br<span style='color:#d2cd86; '>/</span><span style='color:#d2cd86; '>></span>
<span style='color:#b060b0; '>}</span> else <span style='color:#b060b0; '>{</span><span style='color:#d2cd86; '>&lt;</span>br<span style='color:#d2cd86; '>/</span><span style='color:#d2cd86; '>></span>
   <span style='color:#bb7977; font-weight:bold; '>System</span><span style='color:#d2cd86; '>.</span>out<span style='color:#d2cd86; '>.</span>println<span style='color:#d2cd86; '>(</span><span style='color:#00c4c4; '>"dead"</span><span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span><span style='color:#d2cd86; '>&lt;</span>br<span style='color:#d2cd86; '>/</span><span style='color:#d2cd86; '>></span>
<span style='color:#b060b0; '>}</span> <span style='color:#9999a9; '>// END if/else</span>
</pre>
 * @author dca
 */
public class Vital {
   /** The maximum value this vital can be set to.<br/> */
   private int max;

   /** The value which this vital is currently set.<br/> */
   private int cur;

   /**
    * The constructor initializes the max and cur values to match the supplied max value.<br/>
    * @param max The supplied max value.<br/>
    */
   public Vital(int max) {
      max(max);
      cur(max);
   } // END constructor

   public int max() {
      return max;
   } // END max getter

   public Vital max(int num) {
      this.max = num;
      check();
      return this;
   } // END max setter

   public int cur() {
      return cur;
   } // END cur getter

   public Vital cur(int num) {
      this.cur = num;
      check();
      return this;
   } // END cur setter

   public Vital refresh() {
      cur = max;
      return this;
   } // END refresh

   public Vital refresh(int amt) {
      // disallow negative param and overflow
      if(0 < amt && 0 < cur + amt) {
         cur += amt;
         check();
      } // END if
      return this;
   } // END refresh

   public boolean reduce(int amt) {
      // disallow negative param
      if(0 < amt) {
         cur -= amt;
         check();
      } // END if
      return 0 < cur;
   } // END reduce

   public Vital increase(int amt) {
      // disallow negative param and overflow
      if(0 < amt && 0 < cur + amt) {
         max += amt;
      } // END if
      return this;
   } // END increase

   private void check() {
      if(cur < 0) cur = 0;
      if(max < 0) max = 0;
      if(cur > max) cur = max;
   } // END check

   @Override
   public String toString() {
      return String.format("%d/%d", cur, max);
   } // END toString

   @Override
   public int hashCode() {
      int result = 1;

      result = 151 * result + max;
      result = 151 * result + cur;

      return result;
   } // END hashCode

   @Override
   public boolean equals(Object obj) {
      if(this == obj)                       return true;
      if(null == obj)                       return false;
      if(this.getClass() != obj.getClass()) return false;
      Vital v = (Vital)obj;
      return (this.max == v.max && this.cur == v.cur);
   } // END equals
} // END class Vital
